//
//  ZJHSetViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "ZJHSetViewController.h"
#import "FontSizeSingleTon.h"
#import "FitHeader.h"
#import "ZJHWoDeTableViewCell.h"
#define FontSize [[FontSizeSingleTon shareFontSize]array]
#import "CachesManager.h"
@interface ZJHSetViewController ()<UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *titleArray;
@property(nonatomic, strong)NSMutableArray *leftImageArray;
@property(nonatomic, strong)UISwitch *shakeSwitch;
@property(nonatomic, strong)UILabel *cachesLable;
@property(nonatomic, strong)UISlider *slider;
@property(nonatomic, strong)NSArray *tempArray;
@property(nonatomic, strong)UILabel *smallLable;
@property(nonatomic, strong)UILabel *largeLable;
@property(nonatomic, assign)CGFloat value;
@end

@implementation ZJHSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
}
-(void)loadView
{
    [super loadView];
    _tempArray = [NSArray arrayWithArray:FontSize];
    //初始化celltitle数组
    _titleArray = [NSMutableArray arrayWithObjects:@"开启摇一摇震动", @"清理缓存", nil];
    
    //初始化cell右侧图标数组
    _leftImageArray = [NSMutableArray arrayWithObjects:@"iconfont-yaoyiyao", @"iconfont-shezhiqingchuhuancun", nil];
    
    //创建界面
    [self createUI];
}

#pragma mark 创建UI
-(void)createUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20 * ht, self.view.frame.size.width, self.view.frame.size.height - 104 - 20 * ht)];
    _tableView.bounces = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UITableView alloc]init];
    [self.view addSubview:_tableView];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"userCell";
    ZJHWoDeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ZJHWoDeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.titleLable.text = _titleArray[indexPath.row];
    cell.leftImageView.image = [UIImage imageNamed:_leftImageArray[indexPath.row]];
    [cell.rightImageView removeFromSuperview];
#pragma mark 给每个cell添加控件和方法
    switch (indexPath.row) {
        
//        case 0:
//        {
//            _smallLable = [[UILabel alloc]initWithFrame:CGRectMake(210 * wt, 10 * ht, 20 * wt, 20 * ht)];
//            _smallLable.text = @"小";
//            _smallLable.font = [UIFont systemFontOfSize:[FontSize[1] integerValue]];
//            [cell addSubview:_smallLable];
//                          
//            _slider = [[UISlider alloc]initWithFrame:CGRectMake(_smallLable.frame.origin.x + _smallLable.frame.size.width + 10 * wt, 15 * ht, 100 * wt, 10 * ht)];
//            _slider.minimumValue = 10;
//            _slider.maximumValue = 100;
//            _slider.value = _value;
//            if (_value == 0) {
//                _slider.value = 50;
//            }
//            _slider.minimumTrackTintColor = [UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1];
//            _slider.maximumTrackTintColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
//            [_slider setThumbImage:[UIImage imageNamed:@"iconfont-zhongxinyuan"] forState:UIControlStateNormal];
//            [_slider addTarget:self action:@selector(sliderAction) forControlEvents:UIControlEventValueChanged];
//            [cell addSubview:_slider];
//            
//            _largeLable = [[UILabel alloc]initWithFrame:CGRectMake(_slider.frame.origin.x + _slider.frame.size.width + 10 * wt, _smallLable.frame.origin.y, _smallLable.frame.size.width, _smallLable.frame.size.height)];
//            _largeLable.text = @"大";
//            _largeLable.font = _smallLable.font;
//            [cell addSubview:_largeLable];
//        }
//            
//            break;
        case 0:
        {
            _shakeSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(320 * wt, 5 * ht, 30 * wt, 30 * ht)];
            _shakeSwitch.tintColor = [UIColor colorWithRed:0.19 green:0.75 blue:0.6 alpha:1];
            _shakeSwitch.transform = CGAffineTransformMakeScale(0.8 * wt * ht, 0.8 * wt * ht);
            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"shake"]) {
                [_shakeSwitch setOn:YES];
            }
            [_shakeSwitch addTarget:self action:@selector(shakeSwitchAction) forControlEvents:UIControlEventValueChanged];
            [cell addSubview:_shakeSwitch];
        }
            break;
        case 1:
        {
            _cachesLable = [[UILabel alloc]initWithFrame:CGRectMake(305 * wt, 5 * ht, 80 * wt, 30 * ht)];
            _cachesLable.textAlignment = 1;
            _cachesLable.font = [UIFont systemFontOfSize:[FontSize[2] integerValue]];
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
            _cachesLable.text = [CachesManager folderSizeAtPath:cachePath];
            [cell addSubview:_cachesLable];
        }
            break;
        default:
            break;
    }
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40 * ht;
}

//#pragma mark 字体大小slider
//-(void)sliderAction
//{   NSLog(@"%.2f",_slider.value / 50.0);
//    _value = _slider.value;
//    NSLog(@"%f", _value);
//    CGFloat fontScale = _slider.value / 50.0;
//    [FontSize removeAllObjects];
//    for (NSString *str in _tempArray) {
//        NSInteger size = [str integerValue];
//        NSString *fontSize = [NSString stringWithFormat:@"%f", size * fontScale];
////                NSLog(@"%@", fontSize);
//        [FontSize addObject:fontSize];
//
//    }
//    
////    [_smallLable removeFromSuperview];
////    [_slider removeFromSuperview];
////    [_largeLable removeFromSuperview];
////    [_shakeSwitch removeFromSuperview];
////    [_cachesLable removeFromSuperview];
////    [_tableView reloadData];
//}
#pragma mark 清理缓存
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"将会清除所有缓存,离线内容以及图片,是否清除" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"清除", nil];
        alert.delegate = self;
        [alert dismissWithClickedButtonIndex:0 animated:YES];
        [alert show];
    }
}
#pragma mark 摇一摇开关方法
-(void)shakeSwitchAction
{
    if (_shakeSwitch.on) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"shake"];
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"shake"];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        
        UIAlertView *succeedAlert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"清除成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        
        _cachesLable.text = [CachesManager clearCaches];
        double delayInSeconds = .5;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            
            [succeedAlert show];
        });
        
        [self performSelector:@selector(dismissAlertView:) withObject:succeedAlert afterDelay:2];
    }
    
}
-(void)dismissAlertView:(UIAlertView *)alert
{
    if (alert) {
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
